" 设置空格键为leader键
let mapleader=" "

" 快速退出插入模式
inoremap jk <ESC>

" 快速替换
noremap \s :%s//g<left><left>

" 保存和退出
noremap <C-Q> :q!<CR>
noremap <C-S> :w!<CR>

" 快速编辑nvim配置文件
noremap <LEADER>rc :e ~/.config/nvim/init.vim<CR>

" 快速重新载入nvim配置文件
noremap re :source $MYVIMRC<CR>

" 快速复制当前行
nnoremap Y y$

" 复制选中内容到系统剪切板
vnoremap Y "+y

" 缩进调整
nnoremap < <<
nnoremap > >>

" 停止搜索高亮
noremap <LEADER><CR> :nohlsearch<CR>

" 分屏
noremap <LEADER>sl :set splitright<CR>:vsplit<CR>
noremap <LEADER>sh :set nosplitright<CR>:vsplit<CR>
noremap <LEADER>sk :set nosplitbelow<CR>:split<CR>
noremap <LEADER>sj :set splitbelow<CR>:split<CR>

" 在窗口移动光标
noremap <LEADER>h <C-w>h
noremap <LEADER>j <C-w>j
noremap <LEADER>k <C-w>k
noremap <LEADER>l <C-w>l

" 标签页管理
noremap tn :tabe<CR>
noremap th :-tabnext<CR>
noremap tl :+tabnext<CR>

" buffer管理
nnoremap <silent> bd        :bd<cr>
nnoremap <silent> bl        :bn<cr>
nnoremap <silent> bh        :bp<cr>
nnoremap <silent> <m-h>     :bp<cr>
nnoremap <silent> <m-l>     :bn<cr>
vnoremap <silent> <m-h>     <esc>:bp<cr>
vnoremap <silent> <m-l>     <esc>:bn<cr>
inoremap <silent> <m-h>     <esc>:bp<cr>
inoremap <silent> <m-l>     <esc>:bn<cr>
" 光标移动
" insert mode
inoremap <C-b> <Left>
inoremap <C-f> <Right>
inoremap <C-n> <Down>
inoremap <C-p> <Up>
inoremap <C-a> <ESC>^i
inoremap <C-e> <ESC>$a
" normal mode
nnoremap <silent> <C-a> ^
nnoremap <silent> <C-e> $
nnoremap <silent> H 5h
nnoremap <silent> J 5j
nnoremap <silent> K 5k
nnoremap <silent> L 5l
" command mode
inoremap <C-b> <Left>
inoremap <C-f> <Right>
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <M-b> <S-Left>
cnoremap <M-w> <S-Right>

inoremap <expr> <CR> InsertMapForEnter()
noremap run :call CompileRunGcc()<CR>

" LeaderF mappings
" 查找文件名
let g:Lf_ShortcutF = "<leader>ff"
" 查找buffer
noremap <leader>fb :<C-U><C-R>=printf("Leaderf buffer %s", "")<CR><CR>
" 查找最件编辑文件
noremap <leader>fm :<C-U><C-R>=printf("Leaderf mru %s", "")<CR><CR>
noremap <leader>ft :<C-U><C-R>=printf("Leaderf bufTag %s", "")<CR><CR>
noremap <leader>fx :<C-U><C-R>=printf("Leaderf command %s", "")<CR><CR>

" 查找行
noremap <leader>fl :<C-U><C-R>=printf("Leaderf line %s", "")<CR><CR>
" 查找文件内容
noremap <leader>fc :<C-U><C-R>=printf("Leaderf rg %s", "")<CR><CR>
" 查找当前光标所在词
noremap <leader>fw :<C-U><C-R>=printf("Leaderf! rg --current-buffer -e %s ", expand("<cword>"))<CR>
" noremap <C-F> :<C-U><C-R>=printf("Leaderf! rg -e %s ", expand("<cword>"))<CR>
" search visually selected text literally
noremap <leader>fs :<C-U><C-R>=printf("Leaderf! rg -F -e %s ", leaderf#Rg#visual())<CR>
noremap <leader>fr :<C-U>Leaderf! rg --recall<CR>
noremap <leader>tr :<C-U><C-R>=printf("Leaderf! gtags -r %s --auto-jump", expand("<cword>"))<CR><CR>
noremap <leader>td :<C-U><C-R>=printf("Leaderf! gtags -d %s --auto-jump", expand("<cword>"))<CR><CR>
noremap <leader>to :<C-U><C-R>=printf("Leaderf! gtags --recall %s", "")<CR><CR>
noremap <leader>tn :<C-U><C-R>=printf("Leaderf gtags --next %s", "")<CR><CR>
noremap <leader>tp :<C-U><C-R>=printf("Leaderf gtags --previous %s", "")<CR><CR>



" other markdown settings

autocmd Filetype markdown inoremap <buffer> <silent> ], <++>
autocmd Filetype markdown inoremap <buffer> <silent> ]f <Esc>/<++><CR>:nohlsearch<CR>c4l
autocmd Filetype markdown nnoremap <buffer> <silent> ]f <Esc>/<++><CR>:nohlsearch<CR>c4l
autocmd Filetype markdown inoremap <buffer> <silent> ]- ---<Enter><Enter>
autocmd Filetype markdown inoremap <buffer> <silent> ]b **** <++><Esc>F*hi
autocmd Filetype markdown inoremap <buffer> <silent> ]x ~~~~ <++><Esc>F~hi
autocmd Filetype markdown inoremap <buffer> <silent> ]i ** <++><Esc>F*i
autocmd Filetype markdown inoremap <buffer> <silent> ]c `` <++><Esc>F`i
autocmd Filetype markdown inoremap <buffer> <silent> ]m ```<Enter>```<Enter><++><Esc>2ka
autocmd Filetype markdown inoremap <buffer> <silent> ]u <u></u><++><Esc>F/hi
autocmd Filetype markdown inoremap <buffer> <silent> ]p ![](<++>) <Enter><++><Esc>kF[a
autocmd Filetype markdown inoremap <buffer> <silent> ]a [](<++>) <++><Esc>F[a
autocmd Filetype markdown inoremap <buffer> <silent> ]1 #<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> ]2 ##<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> ]3 ###<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> ]4 ####<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> ]t <C-R>=strftime("%Y-%m-%d %H:%M:%S")<CR>

autocmd Filetype markdown inoremap <buffer> <silent> 」, <++>
autocmd Filetype markdown inoremap <buffer> <silent> 」f <Esc>/<++><CR>:nohlsearch<CR>c4l
autocmd Filetype markdown nnoremap <buffer> <silent> 」f <Esc>/<++><CR>:nohlsearch<CR>c4l
autocmd Filetype markdown inoremap <buffer> <silent> 」- ---<Enter><Enter>
autocmd Filetype markdown inoremap <buffer> <silent> 」b **** <++><Esc>F*hi
autocmd Filetype markdown inoremap <buffer> <silent> 」x ~~~~ <++><Esc>F~hi
autocmd Filetype markdown inoremap <buffer> <silent> 」i ** <++><Esc>F*i
autocmd Filetype markdown inoremap <buffer> <silent> 」c `` <++><Esc>F`i
autocmd Filetype markdown inoremap <buffer> <silent> 」m ```<Enter>```<Enter><++><Esc>2ka
autocmd Filetype markdown inoremap <buffer> <silent> 」u <u></u><++><Esc>F/hi
autocmd Filetype markdown inoremap <buffer> <silent> 」p ![](<++>) <Enter><++><Esc>kF[a
autocmd Filetype markdown inoremap <buffer> <silent> 」a [](<++>) <++><Esc>F[a
autocmd Filetype markdown inoremap <buffer> <silent> 」1 #<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> 」2 ##<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> 」3 ###<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> 」4 ####<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> 」t <C-R>=strftime("%Y-%m-%d %H:%M:%S")<CR>

" undotree
noremap L :UndotreeToggle<CR>

" tcomment_vim
nmap <LEADER>cn g>c
vmap <LEADER>cn g>
nmap <LEADER>cu g<c
vmap <LEADER>cu g<

" vim-bookmark
" 设置空格键为leader键
let mapleader=" "

" 快速退出插入模式
inoremap jk <ESC>

" 快速替换
noremap \s :%s//g<left><left>

" 保存和退出
noremap <C-Q> :q!<CR>
noremap <C-S> :w!<CR>

" 快速编辑nvim配置文件
noremap <LEADER>rc :e ~/.config/nvim/init.vim<CR>

" 快速重新载入nvim配置文件
noremap re :source $MYVIMRC<CR>

" 快速复制当前行
nnoremap Y y$

" 复制选中内容到系统剪切板
vnoremap Y "+y

" 缩进调整
nnoremap < <<
nnoremap > >>

" 停止搜索高亮
noremap <LEADER><CR> :nohlsearch<CR>

" 分屏
noremap <LEADER>sl :set splitright<CR>:vsplit<CR>
noremap <LEADER>sh :set nosplitright<CR>:vsplit<CR>
noremap <LEADER>sk :set nosplitbelow<CR>:split<CR>
noremap <LEADER>sj :set splitbelow<CR>:split<CR>

" 在窗口移动光标
noremap <LEADER>h <C-w>h
noremap <LEADER>j <C-w>j
noremap <LEADER>k <C-w>k
noremap <LEADER>l <C-w>l

" 标签页管理
noremap tn :tabe<CR>
noremap th :-tabnext<CR>
noremap tl :+tabnext<CR>

" buffer管理
nnoremap <silent> bd        :bd<cr>
nnoremap <silent> bl        :bn<cr>
nnoremap <silent> bh        :bp<cr>
nnoremap <silent> <m-h>     :bp<cr>
nnoremap <silent> <m-l>     :bn<cr>
vnoremap <silent> <m-h>     <esc>:bp<cr>
vnoremap <silent> <m-l>     <esc>:bn<cr>
inoremap <silent> <m-h>     <esc>:bp<cr>
inoremap <silent> <m-l>     <esc>:bn<cr>
" 光标移动
" insert mode
inoremap <C-b> <Left>
inoremap <C-f> <Right>
inoremap <C-n> <Down>
inoremap <C-p> <Up>
inoremap <C-a> <ESC>^i
inoremap <C-e> <ESC>$a
" normal mode
nnoremap <silent> <C-a> ^
nnoremap <silent> <C-e> $
nnoremap <silent> H 5h
nnoremap <silent> J 5j
nnoremap <silent> K 5k
nnoremap <silent> L 5l
" command mode
inoremap <C-b> <Left>
inoremap <C-f> <Right>
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <M-b> <S-Left>
cnoremap <M-w> <S-Right>
inoremap <expr> <CR> InsertMapForEnter()
noremap run :call CompileRunGcc()<CR>



" vim-floatterm mappings
let g:floaterm_keymap_new    = '<F7>'
let g:floaterm_keymap_prev   = '<A-[>'
let g:floaterm_keymap_next   = '<A-]>'
nnoremap R FloatermNew ranger
nnoremap <C-g> FloatermNew lazygit


" other markdown settings
autocmd Filetype markdown inoremap <buffer> <silent> ], <++>
autocmd Filetype markdown inoremap <buffer> <silent> ]f <Esc>/<++><CR>:nohlsearch<CR>c4l
autocmd Filetype markdown nnoremap <buffer> <silent> ]f <Esc>/<++><CR>:nohlsearch<CR>c4l
autocmd Filetype markdown inoremap <buffer> <silent> ]- ---<Enter><Enter>
autocmd Filetype markdown inoremap <buffer> <silent> ]b **** <++><Esc>F*hi
autocmd Filetype markdown inoremap <buffer> <silent> ]x ~~~~ <++><Esc>F~hi
autocmd Filetype markdown inoremap <buffer> <silent> ]i ** <++><Esc>F*i
autocmd Filetype markdown inoremap <buffer> <silent> ]c `` <++><Esc>F`i
autocmd Filetype markdown inoremap <buffer> <silent> ]m ```<Enter>```<Enter><++><Esc>2ka
autocmd Filetype markdown inoremap <buffer> <silent> ]u <u></u><++><Esc>F/hi
autocmd Filetype markdown inoremap <buffer> <silent> ]p ![](<++>) <Enter><++><Esc>kF[a
autocmd Filetype markdown inoremap <buffer> <silent> ]a [](<++>) <++><Esc>F[a
autocmd Filetype markdown inoremap <buffer> <silent> ]1 #<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> ]2 ##<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> ]3 ###<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> ]t <C-R>=strftime("%Y-%m-%d %H:%M:%S")<CR>


autocmd Filetype markdown inoremap <buffer> <silent> 」, <++>
autocmd Filetype markdown inoremap <buffer> <silent> 」f <Esc>/<++><CR>:nohlsearch<CR>c4l
autocmd Filetype markdown nnoremap <buffer> <silent> 」f <Esc>/<++><CR>:nohlsearch<CR>c4l
autocmd Filetype markdown inoremap <buffer> <silent> 」- ---<Enter><Enter>
autocmd Filetype markdown inoremap <buffer> <silent> 」b **** <++><Esc>F*hi
autocmd Filetype markdown inoremap <buffer> <silent> 」x ~~~~ <++><Esc>F~hi
autocmd Filetype markdown inoremap <buffer> <silent> 」i ** <++><Esc>F*i
autocmd Filetype markdown inoremap <buffer> <silent> 」c `` <++><Esc>F`i
autocmd Filetype markdown inoremap <buffer> <silent> 」m ```<Enter>```<Enter><++><Esc>2ka
autocmd Filetype markdown inoremap <buffer> <silent> 」u <u></u><++><Esc>F/hi
autocmd Filetype markdown inoremap <buffer> <silent> 」p ![](<++>) <Enter><++><Esc>kF[a
autocmd Filetype markdown inoremap <buffer> <silent> 」a [](<++>) <++><Esc>F[a
autocmd Filetype markdown inoremap <buffer> <silent> 」1 #<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> 」2 ##<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> 」3 ###<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> 」4 ####<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> <silent> 」t <C-R>=strftime("%Y-%m-%d %H:%M:%S")<CR>


" far.vim settings
noremap <LEADER>r :F  **/*<left><left><left><left><left>
noremap <leader>vs :tabe .vimspector.json<CR>:LoadVimSpectorJsonTemplate<CR>

" vista.vim
nnoremap <silent><nowait> tt :<C-u>Vista!!<cr>

" coc
nmap ts <Plug>(coc-translator-p)
nmap te :CocCommand explorer<CR>
nmap <leader>x  <Plug>(coc-cursors-operator)
" inoremap <silent><expr> <TAB>
" 		\ pumvisible() ? "\<C-n>" :
" 		\ <SID>check_back_space() ? "\<TAB>" :
" 		\ coc#refresh()
nmap <silent> gd <Plug>(coc-definition) " 跳转到定义
nmap <silent> gi <Plug>(coc-implementation) "跳转到实现
nmap <silent> gr <Plug>(coc-references) "跳转到引用
nmap <silent> gty <Plug>(coc-type-definition) " 跳转到类型定义
nmap <silent> gne <Plug>(coc-diagnostic-next) 
nmap <silent> gle <Plug>(coc-diagnostic-prev)
nmap <leader>rn <Plug>(coc-rename) " 重命名变量

nnoremap <silent> <leader>0 :call <SID>show_documentation()<CR> "预览窗口查看帮助文档"

" 变量重命名
nmap <leader>rn <Plug>(coc-rename)

" 格式化选中的代码
xmap <leader>F  <Plug>(coc-format-selected)
nmap <leader>F  <Plug>(coc-format-selected)
xmap <leader>a  <Plug>(coc-codeaction-selected)
nmap <leader>aw  <Plug>(coc-codeaction-selected)w
nmap <space>ed :CocCommand explorer --preset .vim<CR>
nmap <space>ef :CocCommand explorer --preset floating<CR>
nmap <space>ec :CocCommand explorer --preset cocConfig<CR>
nmap <space>eb :CocCommand explorer --preset buffer<CR>


" coc-snippets
imap <C-l> <Plug>(coc-snippets-expand)
let g:coc_snippet_next = '<C-n>'
let g:coc_snippet_prev = '<C-p>'
"添加选中文本到snippets
xmap <leader>x  <Plug>(coc-convert-snippet)
imap <C-l> <Plug>(coc-snippets-expand-jump)

nnoremap <silent><nowait> <space>cla  :<C-u>CocList diagnostics<cr>
" Manage extensions.
nnoremap <silent><nowait> <space>cle  :<C-u>CocList extensions<cr>
" Show commands.
nnoremap <silent><nowait> <space>clc  :<C-u>CocList commands<cr>
" Find symbol of current document.
nnoremap <silent><nowait> <space>clo  :<C-u>CocList outline<cr>
" Search workspace symbols.
nnoremap <silent><nowait> <space>cls  :<C-u>CocList -I symbols<cr>


" vim-floatterm mappings
let g:floaterm_keymap_new    = '<F7>'
let g:floaterm_keymap_prev   = '<A-[>'
let g:floaterm_keymap_next   = '<A-]>'
let g:floaterm_keymap_toggle = '<A-=>'
nnoremap R :FloatermNew ranger<CR>
nnoremap <C-g> :FloatermNew lazygit<CR>

